package com.mkj.basic;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

/**
 * @Author Arrebol
 * @Date 2024/8/5 21:43
 * @Project graphical programming
 * @Description：
 * swing组件库提供的边框演示
 */
public class BorderDemo {

    JFrame frame = new JFrame("边框演示");



    public void init() {
        //组装视图
        //修改JFrame为网格布局GridLayout
        frame.setLayout(new GridLayout(2,4));
        //向网格中填充不同的JPanel，并设置边框与内容
        //创建BevelBorder
        Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.GREEN, Color.BLUE, Color.GRAY);
        frame.add(getJPanelWithBorder(bevelBorder,"bevelBorder"));
        //创建LineBorder
        Border lineBorder = BorderFactory.createLineBorder(Color.ORANGE, 10);
        frame.add(getJPanelWithBorder(lineBorder,"lineBorder"));
        //创建EmptyBorder
        Border emptyBorder = BorderFactory.createEmptyBorder(10,5,20, 10);
        frame.add(getJPanelWithBorder(emptyBorder,"emptyBorder"));
        //创建EtchedBorder
        Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.RED,Color.GREEN);
        frame.add(getJPanelWithBorder(etchedBorder,"etchedBorder"));
        //创建TitledBorder
        TitledBorder titledBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.ORANGE,10),"测试标题",TitledBorder.LEFT,TitledBorder.BOTTOM,new Font("StSong",Font.BOLD,18),Color.BLUE);
        frame.add(getJPanelWithBorder(titledBorder,"titledBorder"));
        //创建MatteBorder下
        MatteBorder matteBorder = BorderFactory.createMatteBorder(10,5,20,10,Color.GREEN);
        frame.add(getJPanelWithBorder(matteBorder,"matteBorder"));
        //创建CompoundBorder
        CompoundBorder compoundBorder = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.RED,10),titledBorder);
        frame.add(getJPanelWithBorder(compoundBorder,"compoundBorder"));
        //处理关闭动作
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口最佳大小并设置窗口可见
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * 获取一个有边框的JPanel对象
     * @param border        需要设置的边框类型
     * @param context       JPanel中展示的内容
     * @return              有指定边框并展示指定内容的JPanel对象
     */
    public JPanel getJPanelWithBorder(Border border,String context){
        JPanel panel=new JPanel();
        panel.add(new JLabel(context));
        panel.setBorder(border);
        return panel;
    }
    public static void main(String[] args) {
        new BorderDemo().init();
    }
}
